#include "client.h"
#include "ui_client.h"
#include "QDebug"
#include "QTabBar"
#include "QMessageBox"
#include "QRandomGenerator"
#include "tcptaskthread.h"
#include "tcphelper.h"
#include "home.h"

client::client(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::client)
{
    ui->setupUi(this);
    ui->tabWidget->tabBar()->hide();

}

client::~client()
{
    delete ui;
}

User client::generateUser(QString userName, QString userPassword) {
    User newUser;

    // 随机生成用户ID
    myId = QRandomGenerator::global()->bounded(100000, 1000000);

    newUser.id = myId;
    newUser.ip = "127.0.0.1";
    newUser.user_name = userName;
    newUser.user_password = userPassword;
    newUser.user_status = UserStatus::NOT_ONLINE;

    return newUser;
}

void client::on_toolButton_login_clicked()
{
    ui->lineEdit_name->clear();
    ui->lineEdit_passwordone->clear();
    ui->lineEdit_passwordtwo->clear();
    ui->lineEdit_name->setFocus();
    ui->tabWidget->setCurrentIndex(0); //登录界面
}


void client::on_toolButton_register_clicked()
{
    ui->lineEdit_userID->clear();
    ui->lineEdit_userPassword->clear();
    ui->lineEdit_userID->setFocus();
    ui->tabWidget->setCurrentIndex(1); //注册界面
}


void client::on_toolButton_return_clicked()
{
    ui->lineEdit_name->clear();
    ui->lineEdit_passwordone->clear();
    ui->lineEdit_passwordtwo->clear();
    ui->lineEdit_name->setFocus();
    ui->tabWidget->setCurrentIndex(0); //登录界面
}


void client::on_pushButton_confirmLogin_clicked()
{

    if(ui->lineEdit_userID->text()!=""&& ui->lineEdit_userPassword->text()!= "")
    {
        TcpHelper helper;

        // 创建并启动 TcpTaskThread
        TcpTaskThread *taskThread = new TcpTaskThread();
        taskThread->start();

        // 获取 TcpTask 对象
        TcpTask *task2 = taskThread->getTcpTask();

        client::connect(task2, &TcpTask::UserInformationReady, [=](User user){
            if(user.user_password == ui->lineEdit_userPassword->text())
            {//登陆成功
                TcpHelper helper;

                // 创建并启动 TcpTaskThread
                TcpTaskThread *taskThread = new TcpTaskThread();
                taskThread->start();

                // 获取 TcpTask 对象
                TcpTask *task3 = taskThread->getTcpTask();

                user.user_status = ONLINE;

                task3->sendCommand(helper.getUpdateUserInformationTask(user));

                myId = user.id;
                me = user;
                this->close();
                home *hom = new home();
                hom->show();
            }
            else
            {// 登录失败
            QMessageBox::warning(this, "Warning!", "密码错误", QMessageBox::Yes);
            ui->lineEdit_userPassword->clear();
            ui->lineEdit_userPassword->setFocus();
            }
        });

        client::connect(task2, &TcpTask::UserInformationError, [=](){
            QMessageBox::warning(this, "Warning!", "用户不存在", QMessageBox::Yes);
            ui->lineEdit_userID->clear();
            ui->lineEdit_userPassword->clear();
            ui->lineEdit_userPassword->setFocus();
        });

        task2->sendCommand(helper.getUserIdForInformationTask(ui->lineEdit_userID->text().toInt()));
    }
    else
    {
        QMessageBox::warning(this, "Warning!", "用户名或密码不为空", QMessageBox::Yes);
        ui->lineEdit_userID->clear();
        ui->lineEdit_userPassword->clear();
        ui->lineEdit_userID->setFocus();
    }
}



void client::on_toolButton_confirm_clicked()
{

    if(ui->lineEdit_name->text()!=""&& ui->lineEdit_passwordone->text()!=""&& ui->lineEdit_passwordtwo->text()!="")
    {
        if(ui->lineEdit_passwordone->text()==ui->lineEdit_passwordtwo->text())
        {
            TcpHelper helper;

            // 创建并启动 TcpTaskThread
            TcpTaskThread *taskThread = new TcpTaskThread();
            taskThread->start();

            // 获取 TcpTask 对象
            TcpTask *task1 = taskThread->getTcpTask();

            QString user_name = ui->lineEdit_name->text();
            QString password = ui->lineEdit_passwordone->text();

            User user = generateUser(user_name,password);

            client::connect(task1, &TcpTask::InsertUserOk, [=](){
                int userId = user.id;

                // 弹出消息框提示用户的ID
                QMessageBox::information(this, "注册成功", QString("您的用户ID是：%1").arg(userId), QMessageBox::Yes);

                // 清除输入框内容
                ui->lineEdit_name->clear();
                ui->lineEdit_passwordone->clear();
                ui->lineEdit_passwordtwo->clear();

                // 将焦点设置在用户名输入框上
                ui->lineEdit_name->setFocus();

                // 切换到登录界面
                ui->tabWidget->setCurrentIndex(0);
            });


            client::connect(task1, &TcpTask::InsertUserError, [=](){
                QMessageBox::warning(this, "Warning!", "昵称有重复", QMessageBox::Yes);
                ui->lineEdit_name->clear();
                ui->lineEdit_name->setFocus();
            });

            task1->sendCommand(helper.getInsertUserInformationTask(user));
        }
        else
        {//两次密码不一样
            QMessageBox::warning(this, "Warning!", "两次密码不一样", QMessageBox::Yes);
            ui->lineEdit_passwordone->clear();
            ui->lineEdit_passwordtwo->clear();
            ui->lineEdit_passwordone->setFocus();
        }
    }
    else
    {
        QMessageBox::warning(this, "Warning!", "用户名或密码不为空", QMessageBox::Yes);
        ui->lineEdit_name->clear();
        ui->lineEdit_passwordone->clear();
        ui->lineEdit_passwordtwo->clear();
        ui->lineEdit_name->setFocus();
    }
}

